package com.yeyks.controller.admin;

import com.yeyks.common.appcommon.dto.account.AppLoginAccountDTO;
import com.yeyks.common.appcommon.param.AppLoginOutParam;
import com.yeyks.common.appcommon.param.AppLoginParam;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.utils.IPUtils;
import com.yeyks.common.utils.apputil.redis.RedisService;
import com.yeyks.service.AppLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

import static com.yeyks.common.utils.apputil.rediskey.RedisKey.*;

/**
 * @author Administrator
 */
@Api(tags = "AppLoginController_账户登录接口", description = "账户登录相关接口[登录. 退出登录]")
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppLoginController {

    private final AppLoginService loginService;

    private final RedisService redisService;

    @ApiOperation("账户登录")
    @PostMapping("/login")
    public CommonResult<AppLoginAccountDTO> loginIn(@Valid AppLoginParam appLoginParam,
                                                    HttpServletRequest request) {

        ipMax(request);
        return loginService.loginIn(appLoginParam);
    }

    @ApiOperation(value = "登出功能")
    @PostMapping("/logout")
    public CommonResult logout(@Valid AppLoginOutParam appLoginOutParam) {

        return loginService.logout(appLoginOutParam);
    }

    /**
     * 针对IP进行相关的限制
     * @param request 请求
     */
    private void ipMax(HttpServletRequest request) {
        //1天有效期，同一个IP访问的次数
        String ip = IPUtils.getIpAddr(request);
        String ipAddr = ip.replaceAll(":", ".");
        //10分钟的有效期
        String minuteIpKey = IP_KEY + ":minute:" + ipAddr;
        String count = redisService.get(IP_KEY + ":day:" + ipAddr);
        String minuteCount = redisService.get(minuteIpKey);
        if (StringUtils.isEmpty(count)) {
            redisService.increment(IP_KEY + ":day:" + ipAddr, 1L);
            redisService.increment(minuteIpKey, 1L);
            redisService.expire(IP_KEY + ":day:" + ipAddr, 1L, TimeUnit.DAYS);
            redisService.expire(minuteIpKey, 10L, TimeUnit.MINUTES);
        } else if (Integer.valueOf(count) < IP_COUNT) {
            if (StringUtils.isEmpty(minuteCount)) {
                redisService.increment(minuteIpKey, 1L);
                redisService.expire(minuteIpKey, 10L, TimeUnit.MINUTES);
            } else {
                if (Integer.valueOf(minuteCount) >= MINUTE_IP_COUNT) {
                    throw new OftenFiledException("频繁访问，稍后再试");
                } else {
                    redisService.increment(IP_KEY + ":day:" + ipAddr, 1L);
                    redisService.increment(minuteIpKey, 1L);
                }
            }
        } else {
            throw new OftenFiledException("当前当日IP访问超出限制！");
        }
    }
}
